package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.server.pojo.*;
import com.tanhua.dubbo.server.service.IdService;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:实现类
 *
 * @author: 浩克
 */
@Service(version = "1.0.0")
public class QuanZiApiImpl implements QuanZiApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;

    /**
     * @description:实现发布动态
     *
     * @author: 浩克
     */
    @Override
    public String savePublish(Publish publish) {

        //校验publish对象
        if (publish.getUserId() == null){
            return null;
        }
        //...其他逻辑类似

        try {
            //填充数据
            publish.setId(ObjectId.get());
            publish.setCreated(System.currentTimeMillis()); //发布时间
            publish.setSeeType(1); //查看类型(权限)

            //增加自增长的pid
            publish.setPid(this.idService.creatId("publish", publish.getId().toHexString()));

            //保存动态信息
            this.mongoTemplate.save(publish);

            //写入到相册表中
            Album album = new Album();
            album.setId(ObjectId.get());
            album.setPublishId(publish.getId());  //动态Id
            album.setCreated(System.currentTimeMillis());

            //将相册对象写入到mongodb中
            this.mongoTemplate.save(album, "quanzi_album_" +publish.getUserId());

            //查询当前用户的好友数据,将动态数据写入到好友的时间线
            Query query = new Query(Criteria.where("userId").is(publish.getUserId()));
            List<Users> users = this.mongoTemplate.find(null, Users.class);
            for (Users user : users) {
                //写入数据
                TimeLine timeLine = new TimeLine();
                timeLine.setId(ObjectId.get());
                timeLine.setUserId(publish.getUserId());
                timeLine.setPublishId(publish.getId());
                timeLine.setDate(System.currentTimeMillis());

                //保存
                mongoTemplate.save(timeLine, "quanzi_time_line_" + user.getFriendId());
            }
            return publish.getId().toHexString();
        } catch (Exception e) {
            e.printStackTrace();
            //TODO  事务回滚
        }

        return null;
    }


    /**
     * @description:实现查询好友动态
     *
     * @param userId
     * @param page
     * @param pageSize
     */
    @Override
    public PageInfo<Publish> queryPublishList(Long userId, Integer page, Integer pageSize) {

        Pageable pageable = PageRequest.of(page-1, pageSize, Sort.by(Sort.Order.desc("date")));
        Query query = new Query().with(pageable);

        String tableName = "quanzi_time_line_";
        if (null == userId){
            //查询推荐动态
            tableName += "recommend";
        }else {
            //查询好友动态
            tableName += userId;
        }

        //查询自己的时间线表
        List<TimeLine> timeLines = this.mongoTemplate.find(query, TimeLine.class, tableName);

        List<ObjectId> ids = new ArrayList<>();
        for (TimeLine timeLine : timeLines) {
            ids.add(timeLine.getPublishId());
        }

        Query queryPublish = Query.query(Criteria.where("id").in(ids)).with(Sort.by(Sort.Order.desc("created")));
        //查询动态信息
        List<Publish> publishList = this.mongoTemplate.find(queryPublish, Publish.class);

        //封装分页对象
        PageInfo<Publish> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(0);  //不提供总数
        pageInfo.setRecords(publishList);

        return pageInfo;
    }


    /**
     * @description:动态点赞功能
     *
     * @param userId
     * @param publishId
     * @return boolean
     */
    @Override
    public boolean saveLikeComment(Long userId, String publishId) {
        //判断是否已经点赞,若已点赞就撤销点赞
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(1);
        Query query = Query.query(criteria);
        long count = this.mongoTemplate.count(query, Comment.class);
        if (count > 0){
            return false;
        }

        //未点赞,直接返回类型为1的评论
        return saveComment(userId, publishId, 1, null);
    }


    /**
     * @description:取消评论(根据类型)
     *
     * @param userId
     * @param publishId
     * @param commentType
     * @return boolean
     */
    @Override
    public boolean removeComment(Long userId, String publishId, Integer commentType) {
        //删除条件
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(commentType);
        Query query = Query.query(criteria);

        DeleteResult deleteResult = this.mongoTemplate.remove(query, Comment.class);

        return deleteResult.getDeletedCount() > 0;
    }


    /**
     * @description:动态的喜欢功能
     *
     * @param userId
     * @param publishId
     * @return boolean
     */
    @Override
    public boolean saveLoveComment(Long userId, String publishId) {
        //判断是否已经喜欢,若已点赞就撤销点赞
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(3);
        Query query = Query.query(criteria);
        long count = this.mongoTemplate.count(query, Comment.class);
        if (count > 0){
            return false;
        }

        //未喜欢,直接返回类型为3的评论
        return saveComment(userId, publishId, 3, null);
    }


    /**
     * @description: 保存评论
     *
     * @param userId
     * @param publishId
     * @param type
     * @param content
     * @return boolean
     */
    @Override
    public boolean saveComment(Long userId, String publishId, Integer type, String content) {
        try {
            //填入参数数据
            Comment comment = new Comment();
            comment.setUserId(userId);
            comment.setIsParent(true);
            comment.setContent(content);
            comment.setCommentType(type);
            comment.setCreated(System.currentTimeMillis());
            comment.setId(ObjectId.get());
            comment.setPublishId(new ObjectId(publishId));

            //设置发布人的id
            Publish publish = this.mongoTemplate.findById(comment.getPublishId(), Publish.class);
            if (null != publish){
                comment.setPublishUserId(publish.getUserId());
            }else {
                Video video = this.mongoTemplate.findById(comment.getPublishId(), Video.class);
                if (null != video){
                    comment.setPublishUserId(video.getUserId());
                }
            }

            this.mongoTemplate.save(comment);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @description: 查询评论数量
     *
     * @param publishId
     * @param type
     * @return
     */
    @Override
    public Long queryCommentCount(String publishId, Integer type) {
        //查询条件
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(type);
        Query query = Query.query(criteria);

        return this.mongoTemplate.count(query, Comment.class);
    }

    /**
     * @description: 查询单条动态信息
     *
     * @param publishId
     * @return Publish
     */
    @Override
    public Publish queryPublishById(String publishId) {
        return this.mongoTemplate.findById(new ObjectId(publishId), Publish.class);
    }

    /**
     * @description:查询评论
     *
     * @param publishId
     * @param page
     * @param pageSize
     */
    @Override
    public PageInfo<Comment> queryCommentList(String publishId, Integer page, Integer pageSize) {

        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("created")));

        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(2)).with(pageRequest);

        List<Comment> commentList = this.mongoTemplate.find(query, Comment.class);

        PageInfo<Comment> pageInfo = new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(page);
        pageInfo.setRecords(commentList);

        return pageInfo;
    }

    /**
     * 查询用户的评论数据
     *
     * @return
     */
    @Override
    public PageInfo<Comment> queryCommentListByUser(Long userId, Integer type, Integer page, Integer pageSize) {

        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        Query query = new Query(Criteria
                .where("publishUserId").is(userId)
                .and("commentType").is(type)).with(pageRequest);

        List<Comment> commentList = this.mongoTemplate.find(query, Comment.class);

        PageInfo<Comment> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(commentList);
        pageInfo.setTotal(0); //不提供总数
        return pageInfo;
    }

    @Override
    public List<Publish> queryPublishByPids(List<Long> pids) {
        Query query = new Query(Criteria
                .where("pid").in(pids));
        return this.mongoTemplate.find(query, Publish.class);
    }
}
